package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.Loginfo;
import com.xjh.t.service.ILoginfoService;
import com.xjh.t.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/loginfo")
public class LoginfoController {

    @Autowired
    private ILoginfoService loginfoService;

    @RequestMapping("loadAllLoginfo")
    public DataGridView loadAllLoginfo(LoginfoVo vo)
    {

        IPage<Loginfo> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Loginfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getLoginname()),"loginname",vo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(vo.getLoginip()),"loginip",vo.getLoginip());
        queryWrapper.ge(vo.getStartTime() != null,"logintime",vo.getStartTime());
        queryWrapper.le(vo.getEndTime() != null,"logintime",vo.getEndTime());
        queryWrapper.orderByDesc("logintime");
        this.loginfoService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("deleteLoginfo")
    public ResultObj deleteLoginfo(Integer id)
    {
        try
        {
            this.loginfoService.removeById(id);
            return ResultObj.DELETE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("batchDeleteLoginfo")
    public ResultObj batchDeleteLoginfo(LoginfoVo vo)
    {
        try
        {
//            Collection<Serializable> idlist = new ArrayList<>();
//            for(Integer id:vo.getIds())
//                idlist.add(id);
//            this.loginfoService.removeByIds(idlist);

            this.loginfoService.removeByIds(Arrays.asList(vo.getIds()));
            return ResultObj.DELETE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

